<?php
namespace app\index\model;
use think\Db;
use think\Model;
class Friendorder extends Model
{

    

    

    // 表名
    protected $name = 'news_friend_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text'
    ];

    public function getSexNameAttr($value,$data)
    {
        switch ($value) {
            case 1:
                $name = "男";
                break;
            default:
                $name = "女";
                break;
        }
        return $name;
    }

    public function getAgeAttr($value,$data){
        $ey=date('Y');$em=date('m');$ed=date('d');
        $value = $value ? (is_numeric($value)?:strtotime($value)) : time();
        $ty=date('Y',$value);$tm=date('m',$value);$td=date('d',$value);

        $age=$ey-$ty;
        if($tm>$em||$tm==$em&&$td>$ed){
            $age--;
        }
        return $age;
    }

    public function getMtypeAttr($value, $data){
        switch ($value) {
            case 1:
                $name = "交友";
                break;
            case 2:
                $name = "结婚";
                break;
            case 3:
                $name = "旅游";
                break;
            default:
                $name = "其他";
                break;
        }
        return $name;
    }

    public function getCityTextAttr($value,$data)
    {
        return Db::name('area')->find($value)['name']??$value;
    }

    public function getAreaTextAttr($value,$data)
    {
        return Db::name('area')->find($value)['name']??$value;
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
}
